<?php


namespace app\common\service;

use Endroid\QrCode\QrCode as QrCodeModular;

include './extend/phpqrcode/phpqrcode.php';
use QRcode as QRcodeMo;

class Qrcode extends Base
{
    public static function generateQrcode($text){
//        $ym = date("Ym");
//        $dir= app()->getRootPath()."public/qrcode/code/".$ym;
//        $img_name = time().rand(10000,99999);
//        $pathname = $dir."/" . $img_name . '.png';
        $errorCorrectionLevel = 'L';    //容错级别
        $matrixPointSize = 5;           //生成图片大小
//        header("Content-type: image/png");
        ob_start();
        QRcodeMo::png($text,false,$errorCorrectionLevel, $matrixPointSize, 2);
        $imgstr = 'data:image/png;base64,'.base64_encode(ob_get_contents());
        ob_end_clean();
        return $imgstr;
//        return request()->domain()."/public/qrcode/code/".$ym."/". $img_name . '.png';
    }
    /**
     * @param $text
     * @param int $size
     * @param bool $domain
     * @return string
     * @author: Hhy <jackhhy520@qq.com>
     * @describe:生成普通二维码
     */
    public static function make_qrcode($text,$size=105,$domain=true){
        $img_name = time().rand(10000,99999);
        $ym = date("Ym");
        $dir= app()->getRootPath()."public/qrcode/code/".$ym;
        //判断目录是否存在
        if (!is_dir($dir)) {
            mkdir($dir, 0777, true);
        }
        $pathname = $dir."/" . $img_name . '.png';
        $qrCode = new QrCodeModular();
        $qrCode->setText($text);
        $qrCode->setSize($size);
//        $qrCode->setPadding(15);
//        $qrCode->setErrorCorrection('high');
        $qrCode->setForegroundColor(array('r' => 0, 'g' => 0, 'b' => 0, 'a' => 0));
        $qrCode->setBackgroundColor(array('r' => 255, 'g' => 255, 'b' => 255, 'a' => 0));
//        $qrCode->setImageType(QrCode::IMAGE_TYPE_PNG);
        try {
            $qrCode->writeFile($pathname);
            $str="/qrcode/code/".$ym."/". $img_name . '.png';
            if ($domain){
                return request()->domain().$str;
            }else{
                return $str;
            }
        } catch (\Endroid\QrCode\Exceptions\ImageTypeInvalidException $exception) {
            return "";
        }
    }


    /**
     * @param $text
     * @param $logo
     * @return string
     * @throws Exception
     * @author: Hhy <jackhhy520@qq.com>
     * @describe:生成带logo 的二维码
     */
    public static function QrCodeWithLogo($text,$logo,$domain=true){
        if (!file_exists($logo)){
            exception("logo地址不存在");
        }
        $img_name = time().rand(10000,99999);
        $ym = date("Ym");
        $dir= app()->getRootPath()."public/qrcode/code/".$ym;
        //判断目录是否存在
        if (!is_dir($dir)) {
            mkdir($dir, 0777, true);
        }
        $pathname = $dir."/" . $img_name . '.png';
        $qrCode = new QrCodeModular();
        $qrCode->setText($text);
        $qrCode->setSize(300);
        $qrCode->setLogoPath($logo);
//        $qrCode->setLogo($logo);
        $qrCode->setLogoSize(60,60);
//        $qrCode->setErrorCorrection('high');
//        $qrCode->setErrorCorrectionLevel('high');
        $qrCode->setForegroundColor(array('r' => 0, 'g' => 0, 'b' => 0, 'a' => 0));
        $qrCode->setBackgroundColor(array('r' => 255, 'g' => 255, 'b' => 255, 'a' => 0));
        $qrCode->writeFile($pathname);
        $str="/public/qrcode/code/".$ym."/". $img_name . '.png';
        if ($domain){
            return request()->domain().$str;
        }else{
            return $str;
        }
    }


}